home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 90 / CD Actual 90.iso / Software3D / K-3D / k3d-0.4.2.1 / shaders / k3d_slateroof.sl < prev    next >
Encoding:
Text File  |  2004-07-23  |  7.4 KB  |  203 lines

  1. /* Renamed to PQslateroof.sl for RMR -- talrmr@SpamSucks_pacbell.net */
  2.  
  3. /* 
  4.     slateroof.sl - a surface shader working with slateroofd.sl, to
  5.     introduce a slate color onto roof tiles, making the last row of tiles
  6.     transparent (and black) so that the edge of the roof looks irregular.
  7.     You must bear this in mind when modelling - to avoid the roof ending
  8.     before it reaches the wall which supposedly supports it! The shader
  9.     introduces a random color variation so that one tile will be lighter or
  10.     darker than the next Parameters:
  11.  
  12.    Ka: Coefficient of ambient light;
  13.      Kd: Coefficient of diffuse light;
  14.    sfreq: number of tiles on the s direction
  15.    tfreq: number of tiles in the t direction
  16.      maxadd: the maximum amount of overlap of one tile and another
  17.      ramp: the amount of a tile used for the initial rise to the maximum height
  18.          a value between 0 and 1
  19.      gap: the size of the gap between one tile and the next in the s direction,
  20.          measured as a proportion of the tile (so the value of gap must lie between 0 
  21.          and 1)
  22.      maxcolorvary: the maximum amount by which the color of a slate can vary from the 
  23.          average
  24.      txtscale: a scaling factor for the slate coloration - the larger txtscale the
  25.          the smaller the detail on the tile pattern
  26.      factor: a seed used for adjusting the randomness - if you have two roofs with
  27.          the same sfreq, tfreq change the value of factor to ensure they have a 
  28.          different random patterning. A value > 20 works best
  29.          
  30.  
  31.          
  32.                  
  33.          
  34.      Peter Quint 17/11/2000
  35.  
  36.  
  37.     Nb. This shader would normally be used with the slateroofd shader to
  38.     produce an appropriate displacement and message passing from the
  39.     displacement shader would eliminate the need for a lot of the
  40.     calculations in this shader. Renderdc, however, does not yet support
  41.     message passing, so to produce the test image the code has been
  42.     duplicated here 
  43. */
  44.  
  45.  
  46.  
  47. surface
  48. k3d_slateroof(  float     Ka = .2,
  49.                              Kd = .8,
  50.                        sfreq = 10,
  51.                        tfreq = 10,
  52.                            maxadd = .5,
  53.                             ramp = .1,
  54.                             gap = .05,
  55.                             maxcolorvary = .2,
  56.                             factor = 22.238,
  57.                             txtscale = 1)
  58.     /* sfreq and tfreq must not be below 3 */                   
  59.  
  60. {
  61.    uniform float swidth = 1 / sfreq,         /* Tile width in the s directio */
  62.                  twidth = 1 / tfreq,         /* ditto for the t direction */
  63.                  offset = swidth / 2,        /* the amount by which alternate rows are offset */
  64.                       Km = 1;
  65.    float scoord = s, tcoord = 1 - t;         /* re map the t coord so that the coving is at the top */
  66.     float stile, ttile,                             /* An integer identifying the current tile */
  67.             s_offset, t_offset,                     /* Offsets withing the current tile, between 0, 1 */
  68.             newstile, newscoord,                 /* Used in calculations to identify an overlapping tile */
  69.             cs, ct;                                     /* Coordinates set to the centre of the tile and fed into the noise function */
  70.    float add,                                         /* Add, between 0 and 1, hold the amount the current tile overlaps the next */
  71.             news_offset, 
  72.             disps, dispt, disp, dispo,          /* Displacements - s direction, t direction, final displacement, and 
  73.                                                               displacement for the non-overlapping tile */
  74.             temp_s_offset, 
  75.             temp_t_offset, newadd;             
  76.    uniform     float colorfactor = 3.276 * factor;    /* Used in noise calculations */    
  77.    color     Ct = 0;                                /* A temporary color variable */
  78.     float     colorvary = 0,                        /* the random amount by which a tile is lightened or darkened */
  79.                 newcolorvary, ocolorvary, ns;
  80.  
  81.  
  82.     normal Nf = normalize(faceforward(N,I));
  83.  
  84.  
  85.     /* Work out which tile we are in, and the offset within that tile */
  86.    ttile = tcoord / twidth;
  87.     if (mod(ttile,2) >= 1)
  88.       scoord = scoord + offset; /* Displace alternate rows */
  89.    stile = scoord / swidth;
  90.    s_offset = stile - floor(stile);
  91.    t_offset = ttile - floor(ttile);
  92.    stile = floor(stile);
  93.    ttile = floor(ttile);
  94.  
  95.     /* Work out the displacement assuming the point is in the 
  96.    current tile */
  97.  
  98.  
  99.     /* the noise function is always taken at the same point in the tile 
  100.      * normally the center, except in the short tiles which lie at the
  101.      * start and end of an offset row, where the middle of the left side 
  102.     * of the tile is used */
  103.     ct = ttile + .5;
  104.     cs = stile + .5;
  105.     if ((mod(ct,2) >= 1)  && ((stile == 0) || (stile == sfreq)))
  106.     /* An offset row, so we must deal with the half tiles */
  107.     cs = stile;
  108.     if (ttile < tfreq - 1)
  109.         add = noise(cs * factor, ct * factor) * maxadd;
  110.     else 
  111.         add = 0;
  112.     ocolorvary = (noise(cs * colorfactor, ct * colorfactor) - 1) * 2 * maxcolorvary;
  113.     colorvary = ocolorvary;
  114.     /* now calculate the displacement */
  115.     temp_t_offset = t_offset / (1 + add); 
  116.     if (1 - temp_t_offset <= ramp / (1 + add))
  117.        dispt = Km / 2 + smoothstep(0, ramp / (1 + add), (1 - temp_t_offset)) * Km / 2;
  118.    else
  119.        dispt = Km / 2 + smoothstep( 0, 1 - ramp /(1 + add), temp_t_offset) * Km / 2;
  120.     if (s_offset > .5)
  121.        temp_s_offset = 1 - s_offset;
  122.     else
  123.         temp_s_offset = s_offset;
  124.     if (temp_s_offset < gap * (1 + add))
  125.        disps = smoothstep(0, gap * (1 + add), temp_s_offset) * Km;
  126.     else
  127.        disps = Km;
  128.     dispo = min(disps, dispt);
  129.  
  130.        
  131.  
  132.     /* Work out whether the point is in fact in another tile 
  133.      * storing the new tile in stile, ttile and new offset
  134.      * in s_offset, t_offset */
  135.     if ((t_offset <= maxadd) && (ttile >= 1))
  136.         {
  137.             /* the point might be in another tile */
  138.             if (mod(ttile,2) >= 1)
  139.             /* We are in an offset tile row adjust the scoord appropriately for
  140.              * the row below */
  141.                 newscoord = scoord - offset;
  142.             else
  143.                 newscoord = scoord + offset;
  144.             newstile = newscoord / swidth;
  145.             news_offset = newstile - floor(newstile);
  146.             newstile = floor(newstile);
  147.             /* Now calculate cs, ct the reference point for the noise calculation */                    
  148.             ct = ttile - .5;
  149.             cs = newstile + .5;
  150.             if ((mod(ct,2) >= 1) && ((newstile == 0) || (newstile == sfreq)))
  151.             /* An offset row, so we must deal with the half tiles */
  152.                 cs = newstile;
  153.             newadd = noise(cs * factor, ct * factor) * maxadd;
  154.             newcolorvary =  (noise(cs * colorfactor, ct * colorfactor) - 1) * 2 * maxcolorvary;
  155.             if (t_offset <= newadd)
  156.                 /* if t_offset <= newadd we are actually in the overlapping tile */
  157.                 {
  158.                     /* set parameters to overlapping tile */
  159.                     ttile = ttile - 1;
  160.                     stile = newstile;
  161.                     t_offset = t_offset + 1;
  162.                     s_offset = news_offset;
  163.                     add = newadd;
  164.                     colorvary = newcolorvary; 
  165.                 }
  166.         }
  167.     /* calculate the displacement again */
  168.     t_offset = t_offset / (1 + add); 
  169.     if (1 - t_offset <= ramp / (1 + add))
  170.             dispt = Km / 2 + smoothstep(0, ramp / (1 + add), (1 - t_offset)) * Km / 2;
  171.         else
  172.             dispt = Km / 2 + smoothstep( 0, 1 - ramp /(1 + add), t_offset) * Km / 2;
  173.     if (s_offset > .5)
  174.         s_offset = 1 - s_offset;
  175.     if (s_offset < gap / (1 + add))
  176.         disps = Km * smoothstep(0, gap / (1 + add), s_offset);
  177.     else
  178.         disps = Km;
  179.     disp = min(disps, dispt);
  180.     if (dispo > disp)
  181.         colorvary = ocolorvary;
  182.     /*printf("#dispo = %f, disp = %f, colorvary = %f\n",dispo,disp,colorvary); */
  183.  
  184.     ns = noise( s * txtscale * 59.8534 * sfreq, t * txtscale * 59.8534 * tfreq);
  185.     Ct = color spline(     ns, 
  186.             color( .41, .44, .60),
  187.             color( .65, .70, .82),
  188.             color( .39, .31, .35),
  189.             color( .50, .41, .53),
  190.             color( .47, .47, .63));
  191.     Ct = Ct * (1 + colorvary);
  192.     if (ttile < tfreq - 1) 
  193.         Oi = 1;
  194.     else
  195.         {
  196.             Oi = 0;
  197.             Ct = 0;
  198.         };
  199.     Ci = Ct * (Ka * ambient() + Kd * diffuse(faceforward( normalize(N), I )));
  200.       
  201. }
  202.  
  203.